 /* 
  *  Syntelos XB
  *  Copyright (C) 2006 John Pritchard.
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License as
  *  published by the Free Software Foundation; either version 2 of
  *  the License, or (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  *  02111-1307 USA
  */

package syntelos.xb.op;

/**
 * <p> Child of {@link syntelos.xb.at.Code} for the exception
 * table. </p>
 * 
 * @see syntelos.xb.at.Code
 */
public final class Exceptions 
    extends syntelos.xb.Base
    implements syntelos.xb.XB.XOP
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions");

    /**
     * 
     */
    public final static class Exc 
	extends syntelos.xb.Base
    {
	public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions.exc");

	/**
	 * <p> Start offset PC. </p>
	 */
	public final static class Start
	    extends syntelos.xb.op.Operand.Branch
	{
	    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions.exc.start");

	    public Start(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
		super(doc,name);
	    }
	    public Start(org.w3c.dom.Document doc){
		this(doc,NAME);
	    }
	    public Start(){
		this(DOC_NIL);
	    }

	    protected Exc getExc(){
		return (Exc)this.getParentNode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.Code getAttributeCode(){
		return this.getExc().getAttributeCode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
		return this.getExc().getAttributeLocalVariableTable();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.op.Code getOpCode(){
		return this.getExc().getOpCode();
	    }
	}

	/**
	 * <p> End offset PC. </p>
	 */
	public final static class End
	    extends syntelos.xb.op.Operand.Branch
	{
	    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions.exc.end");

	    public End(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
		super(doc,name);
	    }
	    public End(org.w3c.dom.Document doc){
		this(doc,NAME);
	    }
	    public End(){
		this(DOC_NIL);
	    }

	    protected Exc getExc(){
		return (Exc)this.getParentNode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.Code getAttributeCode(){
		return this.getExc().getAttributeCode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
		return this.getExc().getAttributeLocalVariableTable();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.op.Code getOpCode(){
		return this.getExc().getOpCode();
	    }
	}

	/**
	 * <p> Branch offset PC. </p>
	 */
	public final static class Branch
	    extends syntelos.xb.op.Operand.Branch
	{
	    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions.exc.branch");

	    public Branch(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
		super(doc,name);
	    }
	    public Branch(org.w3c.dom.Document doc){
		this(doc,NAME);
	    }
	    public Branch(){
		this(DOC_NIL);
	    }

	    protected Exc getExc(){
		return (Exc)this.getParentNode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.Code getAttributeCode(){
		return this.getExc().getAttributeCode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
		return this.getExc().getAttributeLocalVariableTable();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.op.Code getOpCode(){
		return this.getExc().getOpCode();
	    }
	}

	/**
	 * <p> Catch offset PC. </p>
	 */
	public final static class Catch
	    extends syntelos.xb.op.Operand.Constant
	{
	    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:exceptions.exc.catch");

	    public Catch(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
		super(doc,name);
	    }
	    public Catch(org.w3c.dom.Document doc){
		this(doc,NAME);
	    }
	    public Catch(){
		this(DOC_NIL);
	    }

	    protected Exc getExc(){
		return (Exc)this.getParentNode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.Code getAttributeCode(){
		return this.getExc().getAttributeCode();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
		return this.getExc().getAttributeLocalVariableTable();
	    }
	    /** @see syntelos.xb.Base
	     */
	    public syntelos.xb.op.Code getOpCode(){
		return this.getExc().getOpCode();
	    }
	}

	private syntelos.iou.intmap instructions = new syntelos.iou.intmap();
    
	public Exc(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
	    super(doc,name);
	}
	public Exc(org.w3c.dom.Document doc){
	    this(doc,NAME);
	}
	public Exc(){
	    this(DOC_NIL);
	}

	public syntelos.xb.op.Exceptions getOpExceptions(){
	    return (syntelos.xb.op.Exceptions)this.getParentNode();
	}
	public syntelos.xb.op.Code getOpCode(){
	    return this.getOpExceptions().getOpCode();
	}
	public syntelos.xb.at.Code getAttributeCode(){
	    return this.getOpExceptions().getAttributeCode();
	}
	public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
	    return this.getOpExceptions().getAttributeLocalVariableTable();
	}

	public Start getStartOperand(){
	    Start operand = (Start)this.getChildByName("exceptions.exc.start");
	    if (null == operand){
		operand = new Start();
		this.appendChild(operand);
	    }
	    return operand;
	}
	public syntelos.xb.Op getStartOp(){
	    return this.getStartOperand().getOperandOp();
	}
	public int getStart(){
	    return this.getStartOperand().getOperand();
	}
	public void setStart(int pc){
	    this.getStartOperand().setOperand(pc);
	}
	public End getEndOperand(){
	    End operand = (End)this.getChildByName("exceptions.exc.end");
	    if (null == operand){
		operand = new End();
		this.appendChild(operand);
	    }
	    return operand;
	}
	public syntelos.xb.Op getEndOp(){
	    return this.getEndOperand().getOperandOp();
	}
	public int getEnd(){
	    return this.getEndOperand().getOperand();
	}
	public void setEnd(int pc){
	    this.getEndOperand().setOperand(pc);
	}
	public Branch getBranchOperand(){
	    Branch operand = (Branch)this.getChildByName("exceptions.exc.branch");
	    if (null == operand){
		operand = new Branch();
		this.appendChild(operand);
	    }
	    return operand;
	}
	public syntelos.xb.Op getBranchOp(){
	    return this.getBranchOperand().getOperandOp();
	}
	public int getBranch(){
	    return this.getBranchOperand().getOperand();
	}
	public void setBranch(int pc){
	    this.getBranchOperand().setOperand(pc);
	}
	public Catch getCatchOperand(){
	    Catch operand = (Catch)this.getChildByName("exceptions.exc.catch");
	    if (null == operand){
		operand = new Catch();
		this.appendChild(operand);
	    }
	    return operand;
	}
	public syntelos.xb.CF.Constants.Class getCatchOperandConst(){
	    return (syntelos.xb.CF.Constants.Class)this.getCatchOperand().getOperandConst();
	}
	public int getCatch(){
	    return this.getCatchOperand().getOperand();
	}
	public void setCatch(int pc){
	    this.getCatchOperand().setOperand(pc);
	}
	public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
	    throws java.io.IOException
	{
	    int pc_start = reader.readUnsignedShort();
	    this.setStart(pc_start);
	    int pc_end = reader.readUnsignedShort();
	    this.setEnd(pc_end);
	    int pc_branch = reader.readUnsignedShort();
	    this.setBranch(pc_branch);
	    int catch_type = reader.readUnsignedShort();
	    this.setCatch(catch_type);
	}
	public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
	    throws java.io.IOException
	{
	    int pc_start = this.getStart();
	    writer.writeShort(pc_start);
	    int pc_end = this.getEnd();
	    writer.writeShort(pc_end);
	    int pc_branch = this.getBranch();
	    writer.writeShort(pc_branch);
	    int catch_type = this.getCatch();
	    writer.writeShort(catch_type);
	}
    }
    
    public Exceptions(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
	super(doc,name);
    }
    public Exceptions(org.w3c.dom.Document doc){
	this(doc,NAME);
    }
    public Exceptions(){
	this(DOC_NIL);
    }

    /** @see syntelos.xb.Base
     */
    public syntelos.xb.at.Code getAttributeCode(){
	return (syntelos.xb.at.Code)this.getParentNode();
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.op.Code getOpCode(){
	return this.getAttributeCode().getOpCode();
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
	return this.getAttributeCode().getAttributeLocalVariableTable();
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.Method getMethod(){
	return this.getAttributeCode().getMethod();
    }
    public int atlen(){
	int count = this.countChildNodes();
	return (2 + (8 * count));
    }
    public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
	throws java.io.IOException
    {
	int count = reader.readUnsignedShort();
	Exc exc;
	for (int cc = 0; cc < count; cc++){
	    exc = new Exc();
	    this.appendChild(exc);
	    exc.readCF(src,reader);
	}
    }
    public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
	throws java.io.IOException
    {
	int count = this.countChildNodes();
	writer.writeShort(count);
	if (0 < count){
	    org.w3c.dom.NodeList children = this.getChildNodes();
	    Exc exc;
	    for (int cc = 0; cc < count; cc++){
		exc = (Exc)children.item(cc);
		exc.writeCF(dst,writer);
	    }
	}
    }
}
